namespace Leey.Net.eTerm
{
    using System;
    using System.Collections;
    using System.Globalization;
    using System.Reflection;
    using System.Text.RegularExpressions;

    public class AirFlights : CollectionBase
    {
        private string flightdate = "";
        public string SourAVHCmd = "";
        private DateTime temp = DateTime.Now;

        public int Add(object value)
        {
            return base.List.Add(value);
        }

        public bool ContainAirFlights(AirFlights afs)
        {
            for (int i = 0; i < afs.Count; i++)
            {
                bool flag = false;
                AirFlight flight = afs[i] as AirFlight;
                int num2 = 0;
                while (num2 < base.Count)
                {
                    AirFlight flight2 = this[i] as AirFlight;
                    if (((flight.AirLine == flight2.AirLine) && (flight.WatchTime == flight2.WatchTime)) && (flight.AirCode == flight2.AirCode))
                    {
                        flag = true;
                        break;
                    }
                    i++;
                }
                if (!flag)
                {
                    return false;
                }
            }
            return true;
        }

        public AirFlight GetFlight(string starttime)
        {
            foreach (AirFlight flight in base.List)
            {
                if (flight.WatchTime.Trim() == starttime.Trim())
                {
                    return flight;
                }
            }
            return null;
        }

        public AirFlights GetFlights(AirFlights afs)
        {
            AirFlights flights = new AirFlights();
            for (int i = 0; i < afs.Count; i++)
            {
                AirFlight flight = afs[i] as AirFlight;
                int num2 = 0;
                while (num2 < base.Count)
                {
                    AirFlight flight2 = this[i] as AirFlight;
                    if (((flight.AirLine == flight2.AirLine) && (flight.WatchTime == flight2.WatchTime)) && (flight.AirCode == flight2.AirCode))
                    {
                        flights.Add(flight2);
                        break;
                    }
                    i++;
                }
            }
            return flights;
        }

        public int IndexOf(object value)
        {
            return base.List.IndexOf(value);
        }

        public void Insert(int index, object value)
        {
            base.List.Insert(index, value);
        }

        public bool IsMarriFlight()
        {
            if (base.Count == 2)
            {
                AirFlight flight = (AirFlight) this[1];
                if (flight.AirLine.Trim().Length == 3)
                {
                    return true;
                }
            }
            return false;
        }

        public void Load(string s)
        {
            base.Clear();
            Match match = new Regex("([0-9]{1,2}[A-Za-z]{3}[0-9]{0,2})").Match(s);
            if (match.Success)
            {
                this.FlightDate = match.Groups[1].ToString().Trim();
                this.temp = DateTime.Parse(match.Groups[1].ToString().Trim(), new CultureInfo("en-US"));
            }
            if (!(((this.ParseCommanAirData(s) || this.ParseMHAirData(s)) || this.ParseCAAirData(s)) || this.ParseNWAirData(s)))
            {
            }
        }

        private bool ParseCAAirData(string s)
        {
            Match match = new Regex(@"[0-9]{1,2}\s{1,20}([A-Z]{3})\s{1,5}([A-Z]{3})\s{1,20}([0-9]{4})\s{1,5}([0-9]{4})((\s[A-Z0-9]{2}(?=\s|\r|\n|\b)(?!\w|\s\d{3}))*)\s{1,5}([A-Z]{2}\s{0,5}\d{3})(.*?)((\s[A-Z0-9]{2}(?=\s|\r|\n|\b)(?!\w|\s\d{3}))+)(.*?)((\s[A-Z0-9]{2}(?=\s|\r|\n|\b)(?!\w|\s\d{3}))+)", RegexOptions.Singleline).Match(s);
            if (!match.Success)
            {
                return false;
            }
            while (match.Success)
            {
                AirFlight flight = new AirFlight();
                flight.AirLine = match.Groups[1].ToString() + match.Groups[2].ToString();
                flight.AirCode = Regex.Replace(match.Groups[7].ToString(), @"\s{1,15}", "");
                flight.BerthsType = match.Groups[5].ToString() + match.Groups[9].ToString() + match.Groups[12].ToString();
                flight.WatchTime = match.Groups[3].ToString();
                flight.ArrivedTime = match.Groups[4].ToString();
                flight.AirType = match.Groups[8].ToString();
                flight.WatchDate = this.temp;
                this.Add(flight);
                match = match.NextMatch();
            }
            return true;
        }

        private bool ParseCommanAirData(string s)
        {
            Match match = new Regex(@"([0-9])[ +-].*?([3A-Z]{2}[0-9A-Z]{3,4})\s+.*?(([A-Z][A-Z0-9]\s)+).*?([A-Z]{6})\s([0-9]{4})\s+([0-9]{4})[+0-9]{0,2}\s+([0-9A-Z]{3}).*?((\s[A-Z][A-Z0-9](?=\s|\r|\n))+)", RegexOptions.Singleline).Match(s);
            if (!match.Success)
            {
                return false;
            }
            while (match.Success)
            {
                AirFlight flight = new AirFlight();
                flight.AirLine = match.Groups[5].ToString().Trim();
                flight.AirCode = match.Groups[2].ToString().Trim();
                flight.BerthsType = match.Groups[3].ToString() + match.Groups[9].ToString();
                flight.WatchTime = match.Groups[6].ToString();
                flight.AirType = match.Groups[8].ToString();
                flight.ArrivedTime = match.Groups[7].ToString();
                flight.WatchDate = this.temp;
                this.Add(flight);
                match = match.NextMatch();
            }
            return true;
        }

        private bool ParseMHAirData(string s)
        {
            Match match = new Regex(@"[0-9]{2}\s{1,3}([A-Z]{3})0([A-Z]{3}).*?-([0-9]{4}).*?-([0-9]{4})\s{1,3}([0-9A-Z]{2})\s{1,3}([0-9]{2,4})\s{1,3}E{0,1}([A-Z0-9]{2,30})[0-9]{3}\s{1,100}([A-Z0-9]{6})", RegexOptions.Singleline).Match(s);
            if (!match.Success)
            {
                return false;
            }
            while (match.Success)
            {
                AirFlight flight = new AirFlight();
                flight.AirLine = match.Groups[1].ToString() + match.Groups[2].ToString();
                flight.AirCode = match.Groups[5].ToString() + match.Groups[6].ToString();
                flight.BerthsType = match.Groups[7].ToString() + match.Groups[8].ToString();
                flight.ArrivedTime = match.Groups[4].ToString();
                flight.WatchTime = match.Groups[3].ToString();
                flight.WatchDate = this.temp;
                this.Add(flight);
                match = match.NextMatch();
            }
            return true;
        }

        private bool ParseNWAirData(string s)
        {
            Match match = new Regex(@"([0-9]{1,2})([3A-Z]{2}\s{1,10}[0-9]{2,4})\s+.*?(([A-Z][A-Z0-9]\s)+).*?([A-Z]{3,6})\s([0-9]{4})\s+([0-9]{4})\s+([0-9A-Z]{3})\s{1,10}([0-9A-Z]{3})\s{1,20}(([A-Z][A-Z0-9]\s{0,2})+)", RegexOptions.Singleline).Match(s);
            if (!match.Success)
            {
                return false;
            }
            while (match.Success)
            {
                AirFlight flight = new AirFlight();
                flight.AirLine = match.Groups[5].ToString();
                flight.AirCode = Regex.Replace(match.Groups[2].ToString(), @"\s{1,15}", "");
                flight.BerthsType = match.Groups[3].ToString() + match.Groups[10].ToString();
                flight.WatchTime = match.Groups[6].ToString();
                flight.WatchDate = this.temp;
                this.Add(flight);
                match = match.NextMatch();
            }
            return true;
        }

        public void Remove(int index)
        {
            base.List.Remove(this[index]);
        }

        public string FlightDate
        {
            get
            {
                return this.flightdate;
            }
            set
            {
                this.flightdate = value;
            }
        }

        public object this[int i]
        {
            get
            {
                return base.List[i];
            }
            set
            {
                base.List[i] = value;
            }
        }
    }
}
